/**
 * Author: Dmitry Ustimov
 * Date: 6/1/13
 * Time: 10:08 PM
 */

package com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.mod_multy_type_actionmap.demo.handlers1_100_requests10;

import com.gmail.dmitryustimov.ms.java7.patternslib.chain_of_responsibility.multyevent.HandleAction;
import com.gmail.dmitryustimov.ms.java7.patternslib.chain_of_responsibility.multyevent.Handler;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class CocreteHandler1<T extends Request> extends Handler<T> {

    private String name;
    protected int level;
    private BigDecimal sum = BigDecimal.valueOf(0);

    public CocreteHandler1(String name, int level) {
        this.level = level;
        this.name = name;
        Map<Class<? extends T>, HandleAction<? extends T>> actions = new HashMap<>();
        actions.put((Class<T>)Request1.class, (HandleAction<T>)this.new Request1Action());
        actions.put((Class<T>)Request2.class, (HandleAction<T>)this.new Request2Action());
        actions.put((Class<T>)Request3.class, (HandleAction<T>)this.new Request3Action());
        actions.put((Class<T>)Request4.class, (HandleAction<T>)this.new Request4Action());
        actions.put((Class<T>)Request5.class, (HandleAction<T>)this.new Request5Action());
        actions.put((Class<T>)Request6.class, (HandleAction<T>)this.new Request6Action());
        actions.put((Class<T>)Request7.class, (HandleAction<T>)this.new Request7Action());
        actions.put((Class<T>)Request8.class, (HandleAction<T>)this.new Request8Action());
        actions.put((Class<T>)Request9.class, (HandleAction<T>)this.new Request9Action());
        actions.put((Class<T>)Request10.class, (HandleAction<T>)this.new Request10Action());
        init(actions);
    }

    private class Request1Action implements HandleAction<Request1> {
        @Override
        public void handle(Request1 request) {
            sum.add(BigDecimal.valueOf(1));
        }
    }

    private class Request2Action implements HandleAction<Request2> {
        @Override
        public void handle(Request2 request) {
            sum.add(BigDecimal.valueOf(2));
        }
    }

    private class Request3Action implements HandleAction<Request3> {
        @Override
        public void handle(Request3 request) {
            sum.add(BigDecimal.valueOf(3));
        }
    }

    private class Request4Action implements HandleAction<Request4> {
        @Override
        public void handle(Request4 request) {
            sum.add(BigDecimal.valueOf(4));
        }
    }

    private class Request5Action implements HandleAction<Request5> {
        @Override
        public void handle(Request5 request) {
            sum.add(BigDecimal.valueOf(5));
        }
    }

    private class Request6Action implements HandleAction<Request6> {
        @Override
        public void handle(Request6 request) {
            sum.add(BigDecimal.valueOf(6));
        }
    }

    private class Request7Action implements HandleAction<Request7> {
        @Override
        public void handle(Request7 request) {
            sum.add(BigDecimal.valueOf(7));
        }
    }

    private class Request8Action implements HandleAction<Request8> {
        @Override
        public void handle(Request8 request) {
            sum.add(BigDecimal.valueOf(8));
        }
    }

    private class Request9Action implements HandleAction<Request9> {
        @Override
        public void handle(Request9 request) {
            sum.add(BigDecimal.valueOf(9));
        }
    }

    private class Request10Action implements HandleAction<Request10> {
        @Override
        public void handle(Request10 request) {
            sum.add(BigDecimal.valueOf(10));
        }
    }

    @Override
    public Boolean canHandle(T request) {
        return request.getRequestLevel() <= level;
    }
}
